ScCompilerGlsl

Compiler that produces Glsl code

Constructors

this
this(uint[] ir)
Undocumented in source.

Members

Functions

addHeaderLine
void addHeaderLine(string str)

Adds a line to be added right after #version in GLSL backend. This is useful for enabling custom extensions which are outside the scope of SPIRV-Cross. This can be combined with variable remapping. A new-line will be added.

flattenBufferBlock
void flattenBufferBlock(uint id)

Legacy GLSL compatibility method. Takes a uniform or push constant variable and flattens it into a (i|u)vec4 arrayN; array instead. For this to work, all types in the block must be the same basic type, e.g. mixing vec2 and vec4 is fine, but mixing int and float is not. The name of the uniform array will be the same as the interface block name.

getPartialSource
string getPartialSource()

Returns the current string held in the conversion buffer. Useful for capturing what has been converted so far when compile() throws an error.

requireExtension
void requireExtension(string ext)

Adds an extension which is required to run this shader, e.g. require_extension("GL_KHR_my_extension");

Properties

options
ScOptionsGlsl options [@property getter]
Undocumented in source. Be warned that the author may not have intended to support it.
options
ScOptionsGlsl options [@property setter]
Undocumented in source. Be warned that the author may not have intended to support it.

Inherited Members

From ScCompiler

~this
~this()
Undocumented in source.
dispose
void dispose()

Dispose native resources held by the compiler. It is called during GC collection, but can be also called manually.

compile
string compile()

After parsing, API users can modify the SPIR-V via reflection and call this to disassemble the SPIR-V into the desired langauage. Sub-classes actually implement this.

getName
string getName(uint id)

Gets the identifier (OpName) of an ID. If not defined, an empty string will be returned.

setDecoration
void setDecoration(uint id, spv.Decoration decoration, uint argument)
setDecorationString
void setDecorationString(uint id, spv.Decoration decoration, string argument)

Applies a decoration to an ID. Effectively injects OpDecorate.

setName
void setName(uint id, string name)

Overrides the identifier OpName of an ID. Identifiers beginning with underscores or identifiers which contain double underscores are reserved by the implementation.

hasDecoration
bool hasDecoration(uint id, spv.Decoration decoration)

Returns whether the decoration has been applied to the ID.

getDecoration
uint getDecoration(uint id, spv.Decoration decoration)
getDecorationString
string getDecorationString(uint id, spv.Decoration decoration)

Gets the value for decorations which take arguments. If the decoration is a boolean (i.e. spv.DecorationNonWritable), 1 will be returned. If decoration doesn't exist or decoration is not recognized, 0 will be returned.

unsetDecoration
void unsetDecoration(uint id, spv.Decoration decoration)

Removes the decoration for an ID.

getType
const(SPIRType)* getType(uint id)

Gets the SPIR-V type associated with ID. Mostly used with Resource::typeId and Resource::baseTypeId to parse the underlying type of a resource.

getTypeFromVariable
const(SPIRType)* getTypeFromVariable(uint id)

Gets the SPIR-V type of a variable.

getNonPointerTypeId
uint getNonPointerTypeId(uint typeId)

Gets the id of SPIR-V type underlying the given typeId, which might be a pointer.

getNonPointerType
const(SPIRType)* getNonPointerType(uint typeId)

Gets the SPIR-V type underlying the given typeId, which might be a pointer.

getStorageClass
spv.StorageClass getStorageClass(uint id)

Gets the underlying storage class for an OpVariable.

getFallbackName
string getFallbackName(uint id)

If get_name() is an empty string, get the fallback name which will be used instead in the disassembled source.

getBlockFallbackName
string getBlockFallbackName(uint id)

If get_name() of a Block struct is an empty string, get the fallback name. This needs to be per-variable as multiple variables can use the same block type.

getMemberName
string getMemberName(uint id, uint index)

Given an OpTypeStruct in ID, obtain the identifier for member number "index". This may be an empty string.

getMemberDecoration
uint getMemberDecoration(uint id, uint index, spv.Decoration decoration)
getMemberDecorationString
string getMemberDecorationString(uint id, uint index, spv.Decoration decoration)

Given an OpTypeStruct in ID, obtain the OpMemberDecoration for member number "index".

setMemberName
void setMemberName(uint id, uint index, string name)

Sets the member identifier for OpTypeStruct ID, member number "index".

getMemberQualifiedName
string getMemberQualifiedName(uint type_id, uint index)

Returns the qualified member identifier for OpTypeStruct ID, member number "index", or an empty string if no qualified alias exists

setMemberQualifiedName
void setMemberQualifiedName(uint type_id, uint index, string name)

Sets the qualified member identifier for OpTypeStruct ID, member number "index".

hasMemberDecoration
bool hasMemberDecoration(uint id, uint index, spv.Decoration decoration)

Returns whether the decoration has been applied to a member of a struct.

setMemberDecoration
void setMemberDecoration(uint id, uint index, spv.Decoration decoration, uint argument)

Similar to setDecoration, but for struct members.

setMemberDecorationString
void setMemberDecorationString(uint id, uint index, spv.Decoration decoration, string argument)
Undocumented in source. Be warned that the author may not have intended to support it.
unsetMemberDecoration
void unsetMemberDecoration(uint id, uint index, spv.Decoration decoration)

Unsets a member decoration, similar to unsetDecoration.

getFallbackMemberName
string getFallbackMemberName(uint index)

Gets the fallback name for a member, similar to getFallbackName.

getActiveBufferRanges
BufferRange[] getActiveBufferRanges(uint id)

Returns a vector of which members of a struct are potentially in use by a SPIR-V shader. The granularity of this analysis is per-member of a struct. This can be used for Buffer (UBO), BufferBlock/StorageBuffer (SSBO) and PushConstant blocks. ID is the Resource::id obtained from get_shader_resources().

getDeclaredStructSize
size_t getDeclaredStructSize(const(SPIRType)* struct_type)

Returns the effective size of a buffer block.

getDeclaredStructSizeRuntimeArray
size_t getDeclaredStructSizeRuntimeArray(const(SPIRType)* structType, size_t arraySize)

Returns the effective size of a buffer block, with a given array size for a runtime array. SSBOs are typically declared as runtime arrays. getDeclaredStructSize() will return 0 for the size. This is not very helpful for applications which might need to know the array stride of its last member. This can be done through the API, but it is not very intuitive how to accomplish this, so here we provide a helper function to query the size of the buffer, assuming that the last member has a certain size. If the buffer does not contain a runtime array, arraySize is ignored, and the function will behave as getDeclaredStructSize(). To get the array stride of the last member, something like: getDeclaredStructSizeRuntimeArray(type, 1) - getDeclaredStructSizeRuntimeArray(type, 0) will work.

getDeclaredStructMemberSize
size_t getDeclaredStructMemberSize(const(SPIRType)* structType, uint index)

Returns the effective size of a buffer block struct member.

getActiveInterfaceVariables
uint[] getActiveInterfaceVariables()

Returns a set of all global variables which are statically accessed by the control flow graph from the current entry point. Only variables which change the interface for a shader are returned, that is, variables with storage class of Input, Output, Uniform, UniformConstant, PushConstant and AtomicCounter storage classes are returned.

setEnabledInterfaceVariables
void setEnabledInterfaceVariables(const(uint)[] activeVariables)

Sets the interface variables which are used during compilation. By default, all variables are used. Once set, compile() will only consider the set in active_variables.

getShaderResources
ShaderResources getShaderResources()

Query shader resources, use ids with reflection interface to modify or query binding points, etc.

getShaderResources
ShaderResources getShaderResources(const(uint)[] activeVariables)

Query shader resources, but only return the variables which are part of active_variables. E.g.: get_shader_resources(get_active_variables()) to only return the variables which are statically accessed.

setRemappedVariableState
void setRemappedVariableState(uint id, bool remapEnable)
getRemappedVariableState
bool getRemappedVariableState(uint id)

Remapped variables are considered built-in variables and a backend will not emit a declaration for this variable. This is mostly useful for making use of builtins which are dependent on extensions.

setSubpassInputRemappedComponents
void setSubpassInputRemappedComponents(uint id, uint components)
getSubpassInputRemappedComponents
uint getSubpassInputRemappedComponents(uint id)

For subpassInput variables which are remapped to plain variables, the number of components in the remapped variable must be specified as the backing type of subpass inputs are opaque.

getEntryPointsAndStages
EntryPoint[] getEntryPointsAndStages()
setEntryPoint
void setEntryPoint(string entry, spv.ExecutionModel executionModel)
renameEntryPoint
void renameEntryPoint(string old_name, string new_name, spv.ExecutionModel executionModel)
getCleansedEntryPointName
string getCleansedEntryPointName(string name, spv.ExecutionModel executionModel)

New variants of entry point query and reflection. Names for entry points in the SPIR-V module may alias if they belong to different execution models. To disambiguate, we must pass along with the entry point names the execution model.

unsetExecutionMode
void unsetExecutionMode(spv.ExecutionMode mode)
Undocumented in source. Be warned that the author may not have intended to support it.
setExecutionMode
void setExecutionMode(spv.ExecutionMode mode, uint arg0, uint arg1, uint arg2)
Undocumented in source. Be warned that the author may not have intended to support it.
getExecutionModeArgument
uint getExecutionModeArgument(spv.ExecutionMode mode, uint index)
getExecutionModel
spv.ExecutionModel getExecutionModel()

Gets argument for an execution mode (LocalSize, Invocations, OutputVertices). For LocalSize, the index argument is used to select the dimension (X = 0, Y = 1, Z = 2). For execution modes which do not have arguments, 0 is returned.

getWorkGroupSizeSpecializationConstants
uint getWorkGroupSizeSpecializationConstants(SpecializationConstant x, SpecializationConstant y, SpecializationConstant z)

In SPIR-V, the compute work group size can be represented by a constant vector, in which case the LocalSize execution mode is ignored.

buildDummySamplerForCombinedImages
uint buildDummySamplerForCombinedImages()

Analyzes all OpImageFetch (texelFetch) opcodes and checks if there are instances where said instruction is used without a combined image sampler. GLSL targets do not support the use of texelFetch without a sampler. To workaround this, we must inject a dummy sampler which can be used to form a sampler2D at the call-site of texelFetch as necessary.

buildCombinedImageSamplers
void buildCombinedImageSamplers()

Analyzes all separate image and samplers used from the currently selected entry point, and re-routes them all to a combined image sampler instead. This is required to "support" separate image samplers in targets which do not natively support this feature, like GLSL/ESSL.

getCombinedImageSamplers
CombinedImageSampler[] getCombinedImageSamplers()

Gets a remapping for the combined image samplers.

getSpecializationConstants
SpecializationConstant[] getSpecializationConstants()

API for querying which specialization constants exist. To modify a specialization constant before compile(), use get_constant(constant.id), then update constants directly in the SPIRConstant data structure. For composite types, the subconstants can be iterated over and modified. constant_type is the SPIRType for the specialization constant, which can be queried to determine which fields in the unions should be poked at.

getCurrentIdBound
uint getCurrentIdBound()
Undocumented in source. Be warned that the author may not have intended to support it.
typeStructMemberOffset
uint typeStructMemberOffset(const(SPIRType)* type, uint index)
typeStructMemberArrayStride
uint typeStructMemberArrayStride(const(SPIRType)* type, uint index)
typeStructMemberMatrixStride
uint typeStructMemberMatrixStride(const(SPIRType)* type, uint index)

API for querying buffer objects. The type passed in here should be the base type of a resource, i.e. getType(resource.baseTypeId) as decorations are set in the basic Block type. The type passed in here must have these decorations set, or an exception is raised. Only UBOs and SSBOs or sub-structs which are part of these buffer types will have these decorations set.

getBinaryOffsetForDecoration
bool getBinaryOffsetForDecoration(uint id, spv.Decoration decoration, uint word_offset)

Gets the offset in SPIR-V words (uint) for a decoration which was originally declared in the SPIR-V binary. The offset will point to one or more uint literals which can be modified in-place before using the SPIR-V binary. Note that adding or removing decorations using the reflection API will not change the behavior of this function. If the decoration was declared, sets the word_offset to an offset into the provided SPIR-V binary buffer and returns true, otherwise, returns false. If the decoration does not have any value attached to it (e.g. DecorationRelaxedPrecision), this function will also return false.

bufferIsHlslCounterBuffer
bool bufferIsHlslCounterBuffer(uint id)

HLSL counter buffer reflection interface. Append/Consume/Increment/Decrement in HLSL is implemented as two "neighbor" buffer objects where one buffer implements the storage, and a single buffer containing just a lone "int" implements the counter. To SPIR-V these will be exposed as two separate buffers, but glslang HLSL frontend emits a special indentifier which lets us link the two buffers together.

bufferGetHlslCounterBuffer
bool bufferGetHlslCounterBuffer(uint id, uint counterId)

Queries if a buffer object has a neighbor "counter" buffer. If so, the ID of that counter buffer will be returned in counter_id. If SPV_GOOGLE_hlsl_functionality1 is used, this can be used even with a stripped SPIR-V module. Otherwise, this query is purely based on OpName identifiers as found in the SPIR-V module, and will only return true if OpSource was reported HLSL. To rely on this functionality, ensure that the SPIR-V module is not stripped.

getDeclaredCapabilities
spv.Capability[] getDeclaredCapabilities()

Gets the list of all SPIR-V Capabilities which were declared in the SPIR-V module.

getDeclaredExtensions
string[] getDeclaredExtensions()

Gets the list of all SPIR-V extensions which were declared in the SPIR-V module.

getRemappedDeclaredBlockName
string getRemappedDeclaredBlockName(uint id)

When declaring buffer blocks in GLSL, the name declared in the GLSL source might not be the same as the name declared in the SPIR-V module due to naming conflicts. In this case, SPIRV-Cross needs to find a fallback-name, and it might only be possible to know this name after compiling to GLSL. This is particularly important for HLSL input and UAVs which tends to reuse the same block type for multiple distinct blocks. For these cases it is not possible to modify the name of the type itself because it might be unique. Instead, you can use this interface to check after compilation which name was actually used if your input SPIR-V tends to have this problem. For other names like remapped names for variables, etc, it's generally enough to query the name of the variables after compiling, block names are an exception to this rule. ID is the name of a variable as returned by Resource::id, and must be a variable with a Block-like type.

Meta